package com.flash.javamate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;

/**
 * @author sunyiming
 * @date 2018/8/2
 */
public class DHCPDemo {

    private static DatagramSocket socket;
    private DatagramPacket packet;
    private int port = 68;
    private String addr = "10.3.23.254";

    public static void main(String agrs[]){
        DHCPDemo client = new DHCPDemo();
        try {
            socket = new DatagramSocket();
            while (true){
                client.sendMessage();
                client.receivMessage();
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }


    }
    public void sendMessage(){
        try {
            byte[] datas = "hello this is client echo".getBytes();
            packet = new DatagramPacket(datas,datas.length, InetAddress.getByName(addr),port);
            socket.send(packet);

        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void receivMessage(){
        try {
            byte[] data = new byte[100];
            packet = new DatagramPacket(data,data.length);
            socket.receive(packet);
            System.out.println(new String(packet.getData(),0,packet.getLength()));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
